package com.yjp;

import com.yjp.mapper.OrderMapper;
import com.yjp.mapper.UserMapper;
import com.yjp.service.UserService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.spring.mybatis.YjpBeanFactory;
import org.spring.mybatis.YjpImportBeanDefinitionRegister;
import org.spring.mybatis.YjpMapperScan;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;

import java.io.IOException;
import java.io.InputStream;

@ComponentScan("com.yjp")
@YjpMapperScan("com.yjp.mapper")
public class YjpApplication {
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws IOException{
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        return  sqlSessionFactory;
    }
//    @Bean
//    public UserMapper userMapper(SqlSessionFactory sqlSessionFactory){
//        sqlSessionFactory.getConfiguration().addMapper(UserMapper.class);
//        SqlSession sqlSession = sqlSessionFactory.openSession();
//        return sqlSession.getMapper(UserMapper.class);
//
//    }
//    @Bean
//    public OrderMapper orderMapper(SqlSessionFactory sqlSessionFactory){
//        sqlSessionFactory.getConfiguration().addMapper(OrderMapper.class);
//        SqlSession sqlSession = sqlSessionFactory.openSession();
//        return sqlSession.getMapper(OrderMapper.class);
//
//    }
    public static void main(String[] args){
//        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(YjpApplication.class);
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(YjpApplication.class);
//        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
//        beanDefinition.setBeanClass(YjpBeanFactory.class);
//        beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(UserMapper.class);
//        context.registerBeanDefinition("userMapper",beanDefinition);
//
//        AbstractBeanDefinition beanDefinition1 = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
//        beanDefinition1.setBeanClass(YjpBeanFactory.class);
//        beanDefinition1.getConstructorArgumentValues().addGenericArgumentValue(OrderMapper.class);
//        context.registerBeanDefinition("orderMapper",beanDefinition1);

        context.refresh();
        UserService userService = context.getBean("userService", UserService.class);
        userService.test();
//        System.out.println(context.getBean("yjpBeanFactory"));
//        System.out.println(context.getBean("&yjpBeanFactory"));

    }
}
